/*******************************************************************************
 * Copyright (c) 2012 Alexandr Tsvetkov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Alexandr Tsvetkov - initial API and implementation
 * 
 * Project:
 *     TAO Library
 * 
 * File name:
 *     IconMenuAdapter.java
 *     
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 ******************************************************************************/
package ua.at.tsvetkov.ui.menus;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net Адаптер для меню с иконками
 */
public class IconMenuAdapter extends BaseAdapter {
	private static final int					LIST_PREFERED_HEIGHT	= 65;

	private Context								context					= null;
	private ArrayList<IconContextMenuItem>	mItems					= new ArrayList<IconContextMenuItem>();

	public IconMenuAdapter(Activity context) {
		this.context = context;
	}

	/**
	 * add item to adapter
	 * 
	 * @param menuItem
	 */
	public void addItem(IconContextMenuItem menuItem) {
		mItems.add(menuItem);
	}

	public int getCount() {
		return mItems.size();
	}

	public Object getItem(int position) {
		return mItems.get(position);
	}

	public long getItemId(int position) {
		IconContextMenuItem item = (IconContextMenuItem) getItem(position);
		return item.actionTag;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		IconContextMenuItem item = (IconContextMenuItem) getItem(position);

		Resources res = context.getResources();

		if (convertView == null) {
			TextView temp = new TextView(context);
			@SuppressWarnings("deprecation")
			AbsListView.LayoutParams param = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT,
					AbsListView.LayoutParams.WRAP_CONTENT);
			temp.setLayoutParams(param);
			temp.setPadding((int) toPixel(res, 15), 0, (int) toPixel(res, 15), 0);
			temp.setGravity(android.view.Gravity.CENTER_VERTICAL);

			Theme th = context.getTheme();
			TypedValue tv = new TypedValue();

			if (th.resolveAttribute(android.R.attr.textAppearanceLargeInverse, tv, true)) {
				temp.setTextAppearance(context, tv.resourceId);
			}

			temp.setMinHeight(LIST_PREFERED_HEIGHT);
			temp.setCompoundDrawablePadding((int) toPixel(res, 14));
			convertView = temp;
		}

		TextView textView = (TextView) convertView;
		textView.setTag(item);
		textView.setText(item.text);
		textView.setCompoundDrawablesWithIntrinsicBounds(item.image, null, null, null);

		return textView;
	}

	private float toPixel(Resources res, int dip) {
		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, res.getDisplayMetrics());
		return px;
	}
}

/**
 * menu-like list item with icon
 */
class IconContextMenuItem {
	public final CharSequence	text;
	public final Drawable		image;
	public final int				actionTag;

	/**
	 * public constructor
	 * 
	 * @param res resource handler
	 * @param textResourceId id of title in resource
	 * @param imageResourceId id of icon in resource
	 * @param actionTag indicate action of menu item
	 */
	public IconContextMenuItem(Resources res, int textResourceId, int imageResourceId, int actionTag) {
		text = res.getString(textResourceId);
		if (imageResourceId != -1) {
			image = res.getDrawable(imageResourceId);
		} else {
			image = null;
		}
		this.actionTag = actionTag;
	}

	/**
	 * public constructor
	 * 
	 * @param res resource handler
	 * @param title menu item title
	 * @param imageResourceId id of icon in resource
	 * @param actionTag indicate action of menu item
	 */
	public IconContextMenuItem(Resources res, CharSequence title, int imageResourceId, int actionTag) {
		text = title;
		if (imageResourceId != -1) {
			image = res.getDrawable(imageResourceId);
		} else {
			image = null;
		}
		this.actionTag = actionTag;
	}
}
